/**
 * 可调控的日志工具
 */
export class Logger {

    static NONE = 0;
    static LOG = 1 << 0;
    static INFO = 1 << 1;
    static WARN = 1 << 2;
    static ERROR = 1 << 3;
    static TRACE = 1 << 4;
    static ALL = Logger.LOG | Logger.INFO | Logger.WARN | Logger.ERROR | Logger.TRACE;

    constructor(
        /** 掩码 决定哪些级别的log会输出 */
        mask?: number | (() => number)
    ) {
        this.setMask(mask);
    }

    getMask: () => number;

    setMask(mask: number | (() => number) = Logger.ALL) {
        this.getMask = typeof mask === "number" ? () => mask : mask;
    }

    checkMask(type = Logger.ALL) {
        return (this.getMask() & type) > 0;
    }

    log(...data: any[]) {
        if (this.checkMask(Logger.LOG)) {
            console.log(...data);
        }
    }

    info(...data: any[]) {
        if (this.checkMask(Logger.INFO)) {
            console.info(...data);
        }
    }

    warn(...data: any[]) {
        if (this.checkMask(Logger.WARN)) {
            console.warn(...data);
        }
    }

    error(...data: any[]) {
        if (this.checkMask(Logger.ERROR)) {
            console.error(...data);
        }
    }

    trace(...data: any[]) {
        if (this.checkMask(Logger.TRACE)) {
            console.trace(...data);
        }
    }

}